package ui;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JTextField;

import data.*;

import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author lazyman 地图面板
 */
@SuppressWarnings("serial")
public class GameMap extends JFrame {
	// 接收地图的行数和列数
	public JTextField rowsText;
	public JTextField colsText;
	public JTextField fileNameText;
	public JButton btnOk;
	public JButton btnSave;
	public JButton btnRead;

	public JPanel controlPanel;
	private JPanel grapPanel;
	private GridLayout grapPanelGrid;

	//地图矩阵用label显示
	public Label[][] labels;
	// 规定一下每个label的宽度，以此计算面板的大小
	private int labelHigh = 10;
	private int labelWide = 10;
	
	private int rows = 20;
	private int cols = 20;
	
	public GrapData grapData;

	public GameMap(int rows, int cols) {
		this.rows = rows;
		this.cols = cols;
		init();
	}

	public GameMap() {
		init();
	}
	
	private void init() {
		// 初始化组件
		initComponen();

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				dispose();
			}
		});
		grapData = new GrapData();
		grapData.updateGrap(rows, cols);
		setVisible(true);
	}
	
	public void initComponen() {
		controlPanel = new JPanel();
		controlPanel.setBounds(0, 0, 434, 41);
		controlPanel.setLayout(null);

		JLabel lblStart = new JLabel("r");
		lblStart.setBounds(8, 9, 15, 15);
		controlPanel.add(lblStart);

		rowsText = new JTextField(rows + "");
		rowsText.setBounds(33, 6, 33, 21);
		rowsText.setColumns(10);
		controlPanel.add(rowsText);

		JLabel lblWith = new JLabel("c");
		lblWith.setBounds(76, 9, 20, 15);
		controlPanel.add(lblWith);

		colsText = new JTextField(cols + "");
		colsText.setBounds(97, 6, 33, 21);
		colsText.setColumns(10);
		controlPanel.add(colsText);

		btnOk = new JButton("ok");
		btnOk.setBounds(140, 5, 57, 23);
		controlPanel.add(btnOk);

		btnSave = new JButton("save");
		btnSave.setBounds(286, 5, 73, 23);
		controlPanel.add(btnSave);
		
		btnRead = new JButton("read");
		btnRead.setBounds(360, 5, 73, 23);
		controlPanel.add(btnRead);
		
		fileNameText = new JTextField();
		fileNameText.setBounds(207, 6, 57, 21);
		controlPanel.add(fileNameText);
		fileNameText.setColumns(10);

		grapPanel = new JPanel();
		grapPanel.setBounds(0, 43, 0, 0);
		grapPanelGrid = new GridLayout(rows, cols);
		grapPanel.setLayout(grapPanelGrid);
		initLabel();

		getContentPane().setLayout(null);
		setLocationRelativeTo(getOwner());
		getContentPane().add(controlPanel);
		getContentPane().add(grapPanel);
	}

	// 更新地图的行数和列数
	public void updateGrap(int rows, int cols) {
		//如果不过滤完全相同的情况 会出现莫名其妙的错误
		if(this.rows == rows && this.cols == cols){
			return;
		}
		this.rows = rows;
		this.cols = cols;
		grapPanelGrid.setRows(rows);
		grapPanelGrid.setColumns(cols);
		grapData.updateGrap(rows, cols);
		rowsText.setText(rows+"");
		colsText.setText(cols+"");
		initLabel();
		this.repaint();
	}

	//根据地图矩阵 更新label标签颜色 1-black-障碍物 2-red-start 3-yellow-end 4-green-path
	public void updateLabel(int[][] matrix) {
		grapData.setGrap(matrix);
		for(int i = 0;i < matrix.length; ++i) {
			for(int j = 0;j < matrix[i].length; ++j) {
				switch(matrix[i][j]) {
				case 0: break;
				case 1:
					labels[i][j].setBackground(Color.black);
					break;
				case 2:
					labels[i][j].setBackground(Color.red);
					break;
				case 3:
					labels[i][j].setBackground(Color.yellow);
					break;
				case 4:
					labels[i][j].setBackground(Color.green);
					break;	
				}
			}
		}
		repaint();
	}
	
	// 初始化标签组件
	private void initLabel() {
		labels = new Label[rows][cols];
		grapPanel.removeAll();
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				labels[i][j] = new Label(i, j);
				labels[i][j].setSize(labelWide, labelHigh);
				grapPanel.add(labels[i][j]);
			}
		}
		int wide = controlPanel.getWidth() > cols * labelWide ? controlPanel.getWidth() : (cols + 2) * labelWide;
		int high = rows * labelHigh + 100;
		grapPanel.setSize(rows * labelHigh, cols * labelWide);
		this.setSize(wide, high);
	}

	// 设置标签的颜色
	public void setLabelColor(Point p, Color c) {
		if(p == null){
			return;
		}
		labels[p.getY()][p.getX()].setBackground(c);
		labels[p.getY()][p.getX()].repaint();
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getCols() {
		return cols;
	}

	public void setCols(int cols) {
		this.cols = cols;
	}

}
